<?php /* 	These code was generated using phpCIGen v 0.1.a (21/04/2009)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com,
    #songbee	mukhlisona@gmail.com
	#CV. Trust Solution, jl. Saronojiwo 19 Surabaya, http://www.ts.co.id

	+ Module  		: t_payment Model
	+ Description	: For record model process back-end
	+ Filename 		: c_t_payment.php
 	+ creator 		: 
 	+ Created on 06/Mar/2012 12:35:49

*/

class M_t_payment extends CI_Model{

		//constructor
		function M_t_payment() {
			parent::__construct();
		}

		//function for get list record
		function t_payment_list($filter,$start,$end){
			$sql = "SELECT payment_id,
							 t_bill_id,
							 payment_date,
							 payment_type,
							 payment_value,
							 payment_note					  FROM 	 t_payment";

			// For simple search
			if ($filter<>""){
				$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(payment_id) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(t_bill_id) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(payment_date) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(payment_type) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(payment_value) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(payment_note) LIKE '%".addslashes(strtolower($filter))."%' )";
			}

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
                        $limit = $sql." LIMIT ".$start.",".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for create new record
		function t_payment_create($t_bill_id,
				 $payment_date,
				 $payment_type,
				 $payment_value,
				 $payment_note){
			$data = array(
						 "t_bill_id"=>$t_bill_id,						 "payment_date"=>$payment_date,						 "payment_type"=>$payment_type,						 "payment_value"=>$payment_value,						 "payment_note"=>$payment_note					);
			$this->db->insert('t_payment', $data);
			if($this->db->affected_rows())
				return '1';
			else
				return '0';
		}

		//function for update record
		function t_payment_update($payment_id,
				 $t_bill_id,
				 $payment_date,
				 $payment_type,
				 $payment_value,
				 $payment_note){
			$data = array(
						"t_bill_id"=>$t_bill_id,						"payment_date"=>$payment_date,						"payment_type"=>$payment_type,						"payment_value"=>$payment_value,						"payment_note"=>$payment_note					);

			$this->db->where('payment_id', $payment_id);
			$this->db->update('t_payment', $data);
			return '1';
		}

		//fcuntion for delete record
		function t_payment_delete($pkid){
			// You could do some checkups here and return '0' or other error consts.
			// Make a single query to delete all of the t_payments at the same time :
			if(sizeof($pkid)<1){
				return '0';
			} else if (sizeof($pkid) == 1){
				$sql = "DELETE FROM t_payment WHERE payment_id = ".$pkid[0];
				$this->db->query($sql);
			} else {
				$sql = "DELETE FROM t_payment WHERE ";
				for($i = 0; $i < sizeof($pkid); $i++){
					$sql = $sql . "payment_id= ".$pkid[$i];
					if($i<sizeof($pkid)-1){
						$sql = $sql . " OR ";
					}
				}
				$this->db->query($sql);
			}
			if($this->db->affected_rows()>0)
				return '1';
			else
				return '0';
		}

		//function for advanced search record
		function t_payment_search($payment_id,
				 $t_bill_id,
				 $payment_date,
				 $payment_type,
				 $payment_value,
				 $payment_note,$start,$end){
			//full query
			$sql = "SELECT payment_id,
							 t_bill_id,
							 payment_date,
							 payment_type,
							 payment_value,
							 payment_note					  FROM 	 t_payment";

			if($payment_id!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(payment_id) LIKE '%".addslashes(strtolower($payment_id))."%'";
			};
			if($t_bill_id!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(t_bill_id) LIKE '%".addslashes(strtolower($t_bill_id))."%'";
			};
			if($payment_date!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(payment_date) LIKE '%".addslashes(strtolower($payment_date))."%'";
			};
			if($payment_type!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(payment_type) LIKE '%".addslashes(strtolower($payment_type))."%'";
			};
			if($payment_value!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(payment_value) LIKE '%".addslashes(strtolower($payment_value))."%'";
			};
			if($payment_note!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(payment_note) LIKE '%".addslashes(strtolower($payment_note))."%'";
			};

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
                        $limit = $sql." LIMIT ".$start.",".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for print record
		function t_payment_print($payment_id,
				 $t_bill_id,
				 $payment_date,
				 $payment_type,
				 $payment_value,
				 $payment_note,$option,$filter){
			//full query
			$sql = "SELECT payment_id,
							 t_bill_id,
							 payment_date,
							 payment_type,
							 payment_value,
							 payment_note					  FROM 	 t_payment";
			if($option=='LIST'){
				$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(payment_id) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(t_bill_id) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(payment_date) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(payment_type) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(payment_value) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(payment_note) LIKE '%".addslashes(strtoloer($filter))."%' )";
			} else if($option=='SEARCH'){
				if($payment_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_id) LIKE '%".addslashes(strtolower($payment_id))."%'";
				};
				if($t_bill_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(t_bill_id) LIKE '%".addslashes(strtolower($t_bill_id))."%'";
				};
				if($payment_date!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_date) LIKE '%".addslashes(strtolower($payment_date))."%'";
				};
				if($payment_type!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_type) LIKE '%".addslashes(strtolower($payment_type))."%'";
				};
				if($payment_value!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_value) LIKE '%".addslashes(strtolower($payment_value))."%'";
				};
				if($payment_note!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_note) LIKE '%".addslashes(strtolower($payment_note))."%'";
				};
			}
			$query = $this->db->query($sql);
			return $query->result();
		}

		//function  for export to excel
		function t_payment_export_excel($payment_id ,$t_bill_id ,$payment_date ,$payment_type ,$payment_value ,$payment_note ,$option,$filter){
			//full query
			$sql = "SELECT payment_id,
							 t_bill_id,
							 payment_date,
							 payment_type,
							 payment_value,
							 payment_note					  FROM 	 t_payment";
			if($option=='LIST'){
				$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
				$sql .= "(lower(payment_id) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(t_bill_id) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(payment_date) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(payment_type) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(payment_value) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(payment_note) LIKE '%".addslashes(strtolower($filter))."%' )";
			} else if($option=='SEARCH'){
				if($payment_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_id) LIKE '%".addslashes(strtolower($payment_id))."%'";
				};
				if($t_bill_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(t_bill_id) LIKE '%".addslashes(strtolower($t_bill_id))."%'";
				};
				if($payment_date!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_date) LIKE '%".addslashes(strtolower($payment_date))."%'";
				};
				if($payment_type!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_type) LIKE '%".addslashes(strtolower($payment_type))."%'";
				};
				if($payment_value!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_value) LIKE '%".addslashes(strtolower($payment_value))."%'";
				};
				if($payment_note!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_note) LIKE '%".addslashes(strtolower($payment_note))."%'";
				};
			}
			$query = $this->db->query($sql);
			return $sql;
		}

}
?>